package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/03 11:15
 **/
public class Day19_2_Num23_MergeKLists {
    class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            if(lists.length == 0) return null;
            if(lists.length == 1) return lists[0];
            for(int i = 0;i < lists.length-1;i++){
                lists[i+1] = mergeTwoLists(lists[i],lists[i+1]);
            }
            return lists[lists.length-1];
        }
        // Num21 合并两个有序链表
        public ListNode mergeTwoLists(ListNode list1, ListNode list2){
            if(list1 == null) return list2;
            if(list2 == null) return list1;

            if(list1.val <= list2.val) {
                list1.next = mergeTwoLists(list1.next,list2);
                return list1;
            }else {
                list2.next = mergeTwoLists(list1,list2.next);
                return list2;
            }
        }
    }
}